﻿#include "contentwidget.h"
#include "constvalue.h"
#include "exportexcelobject.h"
#include "excel.h"
#include "tabcontent.h"
#include "commonhelper.h"

#include <QCursor>
#include <QLabel>
#include <QPaintEvent>
#include <QPainter>
#include <QGridLayout>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QComboBox>
#include <QLineEdit>
#include <QTableView>
#include <QPushButton>
#include <QTabWidget>
#include <QSqlQueryModel>
#include <QSqlTableModel>
#include <QHeaderView>

//构造函数
ContentWidget::ContentWidget(QWidget *parent)
    : QWidget(parent)
{
    setMouseTracking(true);

    //设置鼠标样式
    this->setCursor(*new QCursor(QPixmap(DEFAULT_CURSOR), 15, 10));

    this->initWidget();
    this->initParameters();
}

//析构函数
ContentWidget::~ContentWidget()
{
}

void ContentWidget::initWidget()
{
    QVBoxLayout *vLayout = new QVBoxLayout;
    tabWidget = new QTabWidget;
    tabWidget->addTab(new TabContent, "销售明细列表");
    tabWidget->addTab(new TabContent, "销售汇总列表");
    vLayout->addWidget(tabWidget);
    this->setLayout(vLayout);
//    connect(pushbtn, &QPushButton::clicked, this, &ContentWidget::export2excel);
//    QVBoxLayout *vlayout = new QVBoxLayout;
//    vlayout->addWidget(new TabContent);
    //    this->setLayout(vlayout);
}

void ContentWidget::initParameters()
{
    QStringList viewHeaderLst1;
    viewHeaderLst1 << "编号" << "款台号" << "流水号" << "商品编码" << "商品条码" << "商品简称" << "应收价"
                  << "减免" << "实收价" << "数量" << "单位" << "总价" << "支付类型" << "营业员" << "销售时间" << "备注";
    TabContent *t1 = qobject_cast<TabContent*>(tabWidget->widget(0));
    t1->setSheetName("销售明细表");
    QTableView *tableView1 = t1->getTableView();
    QSqlTableModel *model1 = new QSqlTableModel(this);

    model1->setEditStrategy(QSqlTableModel::OnManualSubmit);
    model1->setTable("sales");
    model1->select();
    for (int var = 0; var < model1->columnCount(); ++var) {
        model1->setHeaderData(var, Qt::Horizontal, viewHeaderLst1.at(var));
    }
    tableView1->setModel(model1);
    tableView1->resizeColumnsToContents();
    tableView1->horizontalHeader()->setStretchLastSection(true);

    QStringList viewHeaderLst2;
    viewHeaderLst2 << "编号" << "款台号" << "流水号" << "实际收入" << "实际支付" << "找零"
                   << "营业员" << "销售时间" << "备注";
    TabContent *t2 = qobject_cast<TabContent*>(tabWidget->widget(1));
    t2->setSheetName("销售汇总表");
    QTableView *tableView2 = t2->getTableView();
    QSqlTableModel *model2 = new QSqlTableModel(this);
    model2->setEditStrategy(QSqlTableModel::OnManualSubmit);
    model2->setTable("sales_summary");
    model2->select();
    for (int var = 0; var < model2->columnCount(); ++var) {
        model2->setHeaderData(var, Qt::Horizontal, viewHeaderLst2.at(var));
    }
    tableView2->setModel(model2);
    tableView2->resizeColumnsToContents();
    tableView2->horizontalHeader()->setStretchLastSection(true);
}

//重绘事件
void ContentWidget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QPen objPen(Qt::transparent);
    painter.setPen(objPen);
    QBrush objBrush(QColor(255, 255, 255, 120));
    painter.setBrush(objBrush);
    painter.drawRect(this->rect());
}

